package com.epam.lunch.service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.xml.ws.Endpoint;

import com.epam.lunch.domain.*;
import com.epam.lunch.ws.SimpleOrder;

@WebService
public class LunchWS {
	EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
	EntityManager manager = factory.createEntityManager();

	JpaRestaurantRepository repo = new JpaRestaurantRepository(manager);
	JpaOrderRepository orderRepo = new JpaOrderRepository(manager);
	LunchService cincer = new NetCincer(repo, orderRepo);
	
	public void doOrder(@WebParam(name="order") SimpleOrder order) throws OrderException {
			cincer.order(order);
	}
	
	public List<Restaurant> getAllRestaurants(){
		return repo.getAllRestaurants();
	}
	
	public static void main(String[] args) {
		try {
			String address = InetAddress.getLocalHost().getHostAddress();
			String url = "http://" + address + ":8080/lunch/order";
			System.out.println("### startin WS at:" + url);
			Endpoint.publish(url, new LunchWS());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
